﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class EditAnswerItem 
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                string QuestionId = System.Web.HttpContext.Current.Request["QuestionId"];
                string AnswerId = System.Web.HttpContext.Current.Request["AnswerId"];
                AnswerCls AnswerCls = new QuestionBll().CreateModelAnswer(SiteId,AnswerId);
                string RetHtml =
                     " <script language=javascript> \r\n" +
                     " function SaveAnswerItemText(AnswerId) \r\n" +
                     "   { \r\n" +
                     "       SiteId='" + SiteId + "';\r\n" +
                     "       Title=document.getElementById(\"txtAnsText\").value; \r\n" +
                     "       retValue=Portal.Exam.Render.EditAnswerItem.ServerSideSaveAnsItem(SiteId, AnswerId,Title).value; \r\n" +
                     "       if(retValue.indexOf(\"ERROR\")!=-1) \r\n" +
                     "       {\r\n" +
                     "           alert(retValue); \r\n" +
                     "           return; \r\n" +
                     "       } \r\n" +
                     "       alert(retValue);\r\n"+
                     "       window.history.back(-1);\r\n"+
                     "   } \r\n" +

                     " </script>\r\n"+
                     " <div class=FormSaveUpdate>\r\n"+
                     "<table>" +
                     "      <tr>"+
                     "          <td class=Col1>Nội dung:</td>"+
                     "          <td class=Col2><textarea id=txtAnsText>" + AnswerCls.AnswerTextFirst + "</textarea></td>" +
                     "       </tr>"+
                     "      <tr>" +
                     "          <td></td>" +
                     "          <td><input type=button value=\"Ghi lại\" onclick=\"javascript:SaveAnswerItemText('"+AnswerId+"');\"><input type=button value=\"Đóng\" onclick=javascript:window.history.back(-1);></td>" +
                     "      </tr>" +
                     "</table>";

                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage= ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }
            return OAjaxOut;
        }

        
        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAnsItem(
            string SiteId,
            string AnswerId,
            string Title)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                if (Title.Trim().Length == 0)
                {
                    throw new Exception("Xin vui lòng nhập phương án trả lời!");
                }
                new QuestionBll().SaveAnswerItem(null, SiteId, AnswerId, Title);
                return "Cập nhật phương án trả lời thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}